<?php

global $IP;

require_once( $IP.'/includes/WebStart.php' );
require_once $IP."/extensions/SphinxSearch/sphinxapi.php";
require_once($IP.'/includes/SpecialPage.php');
require_once($IP.'/includes/SearchEngine.php');
require_once($IP.'/includes/SoundShareUtils.php');



/**
 * @name ss_ArtistFiles
 * @author mp2p
 * @abstract class to fetch Related Files to a artis name , filename passed as param
 * to be used on MP3 file single view , and also to be used on wiki pages: artist
 * @param nameFile (int)
 * @param idparent  (int)
 * 
 */
class ss_ArtistFiles {
    function __construct() {  //needed by php 5.2 - Dont remove!
    }

   
  
    public static function ss_ArtistFiles($wgOut, $artist){ //(php 5.2 needs a constructor)
      
      global $wgScript, $wgRequest;
        wfRunHooks('SphinxSearchBeforeResults', array(&$artist, &$page, &$namespaces, &$categories));
            
        $start_time = microtime(true);
        /*$dbr = wfGetDB( DB_SLAVE );
        $sql = "SELECT SQL_CALC_FOUND_ROWS IdFile, Artist, Title, Filename, Size, Bitrate, Length, Album, Tags_count,sum(total) as total, max(s.count) as Count, weight FROM ss_file ss inner join searches s on ss.idfile = s.id WHERE s.query='@artist \"{$artist}\";mode=extended2;limit=1000000;maxmatches=100000;select=*, SUM(Count) as total, idiv(@weight,16777216) as w1;ranker=entity;fieldweights=Filename,1073741825,Title,10,Album,2,Artist,5;sort=extended:Count DESC, w1 DESC, total DESC, @weight DESC, Tags_Count DESC;groupby=attr:Filename_ord;groupsort=Count DESC, w1 DESC, total DESC, @weight DESC, Tags_Count' group by ArtistTitle_ord order by weight&0xFF000000 DESC, total DESC, weight DESC;";
        $resql = $dbr->query( $sql );
        $rescount = $dbr->query("SELECT FOUND_ROWS();"); 
        $total_row = $dbr->fetchrow($rescount); 
        $found = $total_row[0];
        $dbr->freeResult($rescount);
        $preamble = sprintf(wfMsg('sphinxSearchPreamble'), (($page-1)*$wgSphinxSearch_matches+1 > $found) ? $found : ($page-1)*$wgSphinxSearch_matches+1, ($page*$wgSphinxSearch_matches > $found) ? $found : $page*$wgSphinxSearch_matches, $found, $term, 1);
        $wgOut->addWikiText('<hr/><h3>'.$preamble.'</h3>');
        
        while ($row = $dbr->fetchobject($resql)){
                $row->Bitrate = str_replace('~', '', $row->Bitrate);
                $wgOut->addHTML('<div><a href='.$wgScript.'/Special:MP3/'.$row->IdFile.'>'.$row->Filename.'</a><br />');
                $tags = $row->Artist;
                if ($row->Album!="") { if ($tags!="") $tags.=" - "; $tags.=$row->Album;}
                if ($row->Title!="") { if ($tags!="") $tags.=" - "; $tags.=$row->Title;}
                if ($tags!="") $tags = '<div style="font-size:10px">'.strtoupper($tags).'</div>';
                $wgOut->addHTML($tags."Total: {$row->total} {$row->count} ".dechex($row->weight)."<br/>");
                $wgOut->addHTML('Sources: '.$row->Count);
                $wgOut->addHTML(' Size: '.ByteSize($row->Size));
                $wgOut->addHTML(' Length: '.convertS2String($row->Length));
                $wgOut->addHTML(' Bitrate: '.$row->Bitrate);
                $wgOut->addHTML('</div><br/>');
        } 
        $dbr->freeResult($resql);
        */
        wfRunHooks('SphinxSearchAfterResults', array($term, $page));
        
        $wgOut->addWikiText(sprintf(wfMsg('sphinxSearchEpilogue'), microtime(true) - $start_time));
                            
      }
}
